import java.io.*;
import java.net.Socket;


public class ClientHandler implements Runnable {

	private InputStream is;
	private Socket socket;
	private DataStorer dataSaver;

	public ClientHandler(Socket sock, long toc, DataStorer dataSaver) throws IOException {
		socket = sock;
		is = socket.getInputStream();
		this.dataSaver = dataSaver;

		new Thread(this).start();
	}

	@Override
	public void run() {
		boolean allowed = true;		
		MyXMLParser XMLParser = new MyXMLParser(dataSaver);
		MyByteParser ByteParser = new MyByteParser(XMLParser);
		
		while(allowed) {
			try {
				int read = is.read();
				if (read != -1)
					ByteParser.process(read);
				else
					allowed = false;
				}
			catch (Exception e){
				System.err.println("Exception reading from/writing to socket, e="+e);
				e.printStackTrace(System.err);
				return;
			}
		}

		try {
			socket.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}	

	}


}
